package com.doit.demo01;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("all")
public class Bao {

    public static void main(String[] args) {
        Object obj1 = new Object();
        Object obj2 = new Object();
        Object obj3 = new Object();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3,
                                                     3,
                                                      10,
                TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),new ThreadPoolExecutor.AbortPolicy());

        pool.execute(new method2(obj1,obj2,1 ));
        pool.execute(new method2(obj2,obj3,2 ));
        pool.execute(new method2(obj3,obj1,3 ));

//        method(obj1,obj2,1 );
//        method(obj2,obj3,2 );
//        method(obj3,obj1,3 );
    }
    public static void method(Object obj1,Object obj2,int i){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (obj1){
                        synchronized (obj2){
                            System.out.println("线程 "+i+" 运行");
                            obj2.notifyAll();
                        }
                        try {
                            obj1.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
    static class method2 implements Runnable{
        Object obj1;Object obj2;int i;

        public method2(Object obj1, Object obj2, int i) {
            this.obj1 = obj1;
            this.obj2 = obj2;
            this.i = i;
        }
        @Override
        public void run() {
            while (true) {
                synchronized (obj1){
                    synchronized (obj2){
                        System.out.println("线程 "+i+" 运行");
                        obj2.notifyAll();
                    }
                    try {
                        obj1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
